/**
 * Created by wenchuan on 2018/6/15 11:35.
 */
define(function () {

    var self,
        app = win.app,
        detailData,
        characteristics = [],
        gwcxData = {
            GWCX005: '0',
            GWCX006: '0',
            GWCX007: '0',
            GWCX008: '0'
        },
        comments = [],
        imgs = [],
        GWCQ061 = [],
        zlh = [],
        other = {
            GWAM016: '',
            GWAM017: ''
        },
        refreshData = {
            key: 0, //评价key
            status: 0,
            count: 0,
            count1: 0,
            count2: 0,
            count3: 0,
            pv: 0,
            GWAM005: 0, //料号类型
        },
        pageParameters,
        pageUi,
        isUpdate = false
    ;

    var ClassesInfo = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _readyBefore: function () {
                    pageParameters = self._pageParameters;
                    detailData = pageParameters.item;
                    // 拜访记录--添加
                    if (pageParameters.last && pageParameters.last._targer == 'add') {
                        detailData.GWCM003_ = 1;//数量
                        detailData.GWAM001_ = detailData.GWAM001;// 料号
                        detailData.GWAM008_ = detailData.GWAM008;// 单位
                    } else if (pageParameters.type && pageParameters._targer == 'update') {
                        // 拜访记录--修改
                        isUpdate = true;
                        Vue.set(detailData, "GWCM003_", detailData.GWCM003);//数量
                        detailData.GWAP100 = detailData.GWAP100;//单价
                        detailData.GWAM001_ = detailData.GWAM001;// 料号
                        detailData.GWAM008_ = detailData.GWAM008;// 单位
                        if (detailData.GWCQ061) {
                            GWCQ061.pushArray(detailData.GWCQ061.split(','));
                        }
                    }

                    if (!detailData.GWBK008) {
                        Vue.set(detailData, "GWBK008", detailData.GWBK008);
                    }
                }
            }, win);// 放在后边
        },
        addCartData: function (isSubmit) {

            app.showLoading();
            app.uploadLocalIds({
                ids: imgs,
                cb: function (imgs) {
                    //detailData.GWCQ061 = imgs.join(',');//图片地址
                    var imgurl = "";
                    for (var i = 0; i < imgs.length; i++) {
                        imgurl += (imgs[i].name + ",");
                    }
                    detailData.GWCQ061=imgurl.substring(0,imgurl.length-1);
                    detailData.GWCQ003 = detailData.GWCM003_;
                    detailData.GWCQ001 = detailData.GWAM001_;
                    detailData.GWCQ002 = detailData.GWAM008_;// 单位
                    detailData.GWCQ004 = detailData.GWAP100;// 单价
                    app.ajax({
                        xurl: "api/wx/gwat300/addCartData",
                        loading: true,
                        errmsg: isSubmit ? '订单提交异常' : '添加购物车异常',
                        xdata: {
                            obj: app.jsonString(detailData)
                        },
                        cb: function (result) {
                            if (result.ok) {
                                if (isSubmit) {
                                    detailData.GWCQDOCNO = result.data.GWCQDOCNO;
                                    detailData.GWCQSEQ = result.data.GWCQSEQ;
                                    app.openPage({
                                        src: "gwcxd/gwc-submit.html",
                                        argsKey: 'gwcsubmit_page',
                                        args: {
                                            items: [detailData],
                                            cb: function () {
                                                app.back(1);
                                            }
                                        },
                                        // 导致购物车 --提交--选择客户页面 打开不了？？TODO
                                        // cb: function () {
                                        //     app.back(1);
                                        // }
                                    });
                                } else {
                                    app.alert({
                                        msg: '添加成功！',
                                        ok: function () {
                                            if (pageParameters.last) {
                                                pageParameters.last.cb();
                                            } else {

                                                app.back(2);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    });
                }
            });

        },
        subData: function () {
            self.addCartData(true);
        },
        updateOrder: function () { // 购物车--修改
            app.showLoading();
            app.uploadLocalIds({
                imgs: imgs,
                cb: function (imgs) {
                    var _data = {
                        GWCQDOCNO: detailData.GWCQDOCNO,
                        GWCQ001: detailData.GWAM001_,
                        GWCQ002: detailData.GWAM008_,
                        GWCQ003: detailData.GWCM003_,
                        GWCQ004: detailData.GWAP100,
                        GWCQSEQ: detailData.GWCQSEQ,
                        GWCQ060: detailData.GWCQ060,
                        GWCQ016: detailData.GWCQ016,
                        GWCQ061: ''
                    };
                    var imgurl = "";
                    for (var i = 0; i < imgs.length; i++) {
                        imgurl += (imgs[i].name + ",");
                    }
                    //var _imgs = imgs.join(',');//图片地址
                    if (detailData.GWCQ061) {
                        _data.GWCQ061 = detailData.GWCQ061;
                    }
                    if (imgurl != '') {
                        _data.GWCQ061 += ',' + imgurl.substring(0,imgurl.length-1);
                    }
                    app.ajax({
                        xurl: "api/wx/gwat300/updateOrder",
                        loading: true,
                        errmsg: '保存操作异常',
                        xdata: {
                            obj: app.jsonString([_data])
                        },
                        cb: function (result) {
                            if (result.ok) {
                                app.alert({
                                    msg: '订单保存成功',
                                    ok: function () {
                                        detailData.GWCQ001 = detailData.GWAM001_;
                                        detailData.GWCQ002 = detailData.GWAM008_;
                                        detailData.GWCQ003 = detailData.GWCM003_;
                                        detailData.GWCQ004 = detailData.GWAP100;
                                        detailData.GWCQ061 = _data.GWCQ061;
                                        app.back(1);
                                    }
                                });
                            }
                        }
                    });
                }
            });
        },
        getGwcxData: function () {
            app.ajax({
                xurl: "api/wx/gwat300/getGwcxData",
                // xtime: '1d',
                xdata: {
                    gwcx002: detailData.GWAM001_
                },
                cb: function (result) {
                    if (result.ok && result.data.data != null) {
                        Vue.set(gwcxData, 'GWCX005', result.data.data.GWCX005);
                        Vue.set(gwcxData, 'GWCX006', result.data.data.GWCX006);
                        Vue.set(gwcxData, 'GWCX007', result.data.data.GWCX007);
                        Vue.set(gwcxData, 'GWCX008', result.data.data.GWCX008);
                    }
                }
            });
        },
        getGwcwCount: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwcwCount",
                // xtime: '1d',
                xdata: {
                    GWCW020: detailData.GWAM001_,
                    GWAM005: 1,//detailData.GWAM005,
                    status: refreshData.status
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.count;
                        app.extend(refreshData, result.data.count);
                        if (data.count > 0) {
                            refreshData.pv = (refreshData.count1 / refreshData.count * 100).toFixed(2);
                        } else {
                            refreshData.pv = 0;
                        }
                    }
                }
            });
        },
        getGwamOther: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwamOther",
                // xtime: '1d',
                xdata: {
                    GWAM001: detailData.GWAM001_
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        app.extend(other, data);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll3'];
                self._refreshVueData = refreshData;
                self._refreshVueMethods = {
                    statusChange: function () {
                        self._refresh();
                        self.getGwcwCount();
                    },
                    getpj: function (val) {
                        refreshData.key = val;
                        self._refresh();
                    }
                };
                this._super(win);
            } else {
                self._refresh();
            }
            self.getGwcwCount();
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwcw",
                // xtime: '1m',
                // ocache: true,
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWCW020: detailData.GWAM001_,
                    key: refreshData.key,//评论级别
                    GWAM005: 1, //料号类型
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        _super({
                            obj: goods,
                            data: result.data.data,
                            nodata: '暂无评论信息...',
                            nodata_img: 'noneComment.png'
                        });
                    }
                }
            });
        },
        _lazy: function () {

            refreshData.GWAM005 = detailData.GWAM005;

            new Vue({
                el: app.getByid(doc, 'scroll2'),
                data: other,
                methods: {

                }
            });

            $('.wf-infoTab ul li').on('tap', function () {
                $(this).addClass('active').siblings('li').removeClass('active');
                $(this).parents('.wf-infoTab').find('.tabCont .group').eq($(this).index()).show().siblings('.group').hide();
            });

            doc.getElementById('slider').addEventListener('slide', function (e) {
                if (e.detail.slideNumber == 2) {
                    self._initRefresh(win);
                }
            });

            self.getGwamOther();

            app.taps.call(win, [{
                o: 'btnSubmit',
                cb: function (e) {
                    self.subData();
                },
                confirm: {
                    msg: '确定提交订单？'
                }
            }, {
                o: 'btnSave',
                cb: function (e) {
                    self.addCartData();
                },
                confirm: {
                    msg: '确定添加到购物车？'
                }
            }, {
                o: 'btnSave1',
                cb: function (e) {
                    self.updateOrder();
                },
                confirm: {
                    msg: '确定保存商品？'
                }
            }]);
        },
        _ready: function () {
            var scroll1Vue = new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    item: detailData,
                    characteristics: characteristics,
                    gwcxData: gwcxData,
                    comments: comments,
                    imgs: imgs,
                    GWCQ061: GWCQ061,
                    baseUrl: app.cxt()
                },
                methods: {
                    minus: function (val) {
                        if (val.GWCM003_ > 1) {
                            val.GWCM003_--;
                        }
                    },
                    plus: function (val) {
                        val.GWCM003_++;
                    },
                    addImg: function () {
                        var _len = 6 - imgs.length;
                        if (_len > 0) {
                            app.chooseImageNew(function (imgs_) {
                                //imgs.pushArray(imgs_);
                                for (var i = 0; i < imgs_.length; i++) {
                                    imgs.push({
                                        img: imgs_[i].img,
                                        id: imgs_[i].localId
                                    })
                                }
                            }, _len);
                        }
                    },
                    scanCode: function () {
                        app.scanQRCode(function (s) {
                            detailData.GWCQ060 = s;
                        });
                    }
                }
            });

            Vue.component('commentsitem', {
                template: app.getByid(doc, 'comments-template'),
                props: {
                    model: Array
                },
                methods: {}
            });

            pageUi = {
                btnSubmit: $('#btnSubmit'), btnSave: $('#btnSave'), btnSave1: $('#btnSave1')
            };
            if (pageParameters.last && pageParameters.last.type == 'gwc-add') {
                pageUi.btnSave.show();
            } else if (pageParameters.type && pageParameters.type == 'gwc-update') {
                pageUi.btnSave1.show();
            } else {
                pageUi.btnSubmit.show();
                pageUi.btnSave.show();
            }

            app.getCharacteristic({
                gwan001_: detailData.GWAM001_,
                gwam005_: detailData.GWAM005,
                _vue: scroll1Vue,
                cb: function (result) {
                    //用到缓存 会调用2次，不要放跟特征值无关的逻辑

                },
                changeCb: function (obj) {
                    detailData.GWAM001_ = obj.GWAM001;
                    detailData.GWAML003 = obj.GWAML003;
                    detailData.GWAM008_ = obj.GWAP004;
                    detailData.GWAP100 = Number(obj.GWAP100);
                    detailData.GWAM015 = obj.GWAM015;
                    self.getGwcxData();
                    self.getGwamOther();
                }
            });
        }
    });

    new ClassesInfo();

});